﻿
var btnMore = $(".moreti");
var groupList = $(".nfTaglay");
var groupItem = $(".sltmenu");
var btnMagGroup = $("#btnManageGroup");
if (groupItem[0] != null && btnMagGroup[0] != null) {
    groupItem.JQP_ClickOther(function () {
        groupItem.hide();
    });
    btnMagGroup.click(function (e) {
        e.stopPropagation();
        groupItem.show();
    });
}
$("#MIB_creategroup").click(function () {
    App.popGroupDiv();
});

App.lazyEvent(btnMore, "mouseenter", "mouseleave", 500, function () {
    groupList.css("left", $(btnMore).offset().left - 25).css("top", $(btnMore).offset().top + 20).show()
}, function () { App.lazyFunction(function () { groupList.hide(); }, 500) });
App.lazyEvent(groupList, "mouseleave", "mouseenter", 500, function () {
    groupList.hide();
});
var GroupSortCon = '<div class="layerBox"><div class="layerBoxTop"><div class="topCon"><strong>调整分组顺序</strong><a title="关闭" class="close" href="javascript:;"></a><div class="clear"></div></div></div><div class="layerBoxCon" style="width: 440px; height: auto;"><div class="friexpicbor">				  <div class="friexpic">					<h3>首页分组显示预览</h3>					<div class="fbTagB">					  <ul id="ul_oriderLists"><li class="current"><span onclick="MemberShow(this)">我的班级 </span></li><li>更多</li></ul>					 </div>					</div>				</div><div class="friLayBox">                <div class="frileft">                  <ul class="frititle" id="mpop_ul_frititle">                    <li>分组名</li>                  </ul>                  <div class="friList">					<ul id="group_ul_lists">载入中。。。</ul>                  </div>                </div>                <div class="friright">                  <a class="btn_notclick" href="javascript:void(0)" id="orderup"><em><img title="" class="chupdwn_icon dch_icon" src="/Content/Image/transparent.gif">上移</em></a>                  <a class="btn_notclick" href="javascript:void(0)" id="orderdown"><em><img src="/Content/Image/transparent.gif" class="chupdwn_icon uch_icon" title="">下移</em></a>               </div>                <p class="btn" id="mpop_p_btn"><a class="newabtngrn" href="javascript:void(0)" id="gosave"><em>保存</em></a><a class="btn_normal btns" href="javascript:void(0)" id="gocancel"><em>取消</em></a></p>              </div>			</div></div>';
$("#Adjust_group_order").click(function () {
    if (App.initDialDiv("GroupSortBox", dialDiv, false, null)) {
        var groupDiv = $("#GroupSortBox");
        groupDiv.removeClass("4CancelAllLayer");
        groupDiv.find(".mid_c").html(GroupSortCon);
        $("#GroupSortBox .close").add($("#gocancel")).click(function () {
            groupDiv.remove();
            App.hideMash();
            return false;
        });
        var position = App.centerInScreen(groupDiv);
        groupDiv.css("left", position.left + "px");
        groupDiv.css("top", position.top + "px");
        App.showMash(true);
        $.ajax({
            dataType: "json",
            url: "/Ajax/GetAllGroup/",
            cache: false,
            data: { fuid: 0 },
            type: "post",
            success: function (o) {
                if (o.Code == "A00005") {
                    var groupCount = o.Data.length;
                    var up = $("#orderup").click(function () {
                        if (this.className != "btn_notclick") {
                            var sel = list.find(".down").parent();
                            var clone = sel.clone()
                            clone.insertBefore(sel.prev()).find("a").mousedown(mouseDown);
                            sel.remove();
                            preview();
                            enableBtn(clone);
                        }
                        return false;
                    });
                    var down = $("#orderdown").click(function () {
                        if (this.className != "btn_notclick") {
                            var sel = list.find(".down").parent();
                            var clone = sel.clone();
                            clone.insertAfter(sel.next()).find("a").mousedown(mouseDown);
                            sel.remove();
                            preview();
                            enableBtn(clone);
                        }
                        return false;
                    });
                    $("#gosave").click(function () {
                        if (this.locked)
                            return false;
                        this.locked = true;
                        alink = this;
                        var as = list.find("a");
                        var gids = "";
                        for (var i = 0; i < as.length; i++) {
                            gids += $(as[i]).attr("gid") + ",";
                        }
                        $.ajax({
                            dataType: "json",
                            url: "/Ajax/SortGroup/",
                            cache: false,
                            data: { gids: gids },
                            type: "post",
                            success: function (o) {
                                if (o.Code == "A00004") {
                                    App.fullTip(CodeList["A00004"], 1000, window.location.href.replace("#", ""), 3);
                                }
                                else if (o.Code == "A00001") {
                                    App.showLoginDial();
                                }
                                else {
                                    App.fullTip(CodeList["A00003"], 1000, null, 1);
                                }
                                alink.locked = false;
                            },
                            error: function () {
                                App.fullTip(CodeList["A00003"], 1000, null, 1);
                                alink.locked = false;
                            }
                        });
                        return false;
                    });
                    var top = $("#ul_oriderLists");
                    var list = $("#group_ul_lists");
                    var preview = function () {
                        var as = list.find("a");
                        if (as.length <= 3) {
                            for (var i = 0; i < as.length; i++) {
                                $(top.find("li")[2 * i + 1]).text($(as[i]).text());
                            }
                        }
                        else {
                            for (var i = 0; i < 3; i++) {
                                $(top.find("li")[2 * i + 1]).text($(as[i]).text());
                            }
                        }
                    }
                    var enableBtn = function ($elems) {
                        var index = list.find("li").index($elems);
                        if (index == 0) {
                            up.attr("class", "btn_notclick");
                        }
                        else {
                            up.attr("class", "btn_normal");
                        }
                        if (index == groupCount - 1) {
                            down.attr("class", "btn_notclick");
                        }
                        else {
                            down.attr("class", "btn_normal");
                        }
                    }
                    var mouseDown = function (e) {
                        list.find("a").attr("class", "").bind("mousemove", mouseMove);
                        $(".frititle  li").bind("mousemove", mouseMove);
                        this.className = "down";
                        enableBtn($(this).parent());
                        $(document).bind("mouseup", mouseUp);
                        if (this.style)
                            this.style.MozUserSelect = "none";
                        return false;
                    }
                    var mouseMove = function (e) {
                        if ($(e.target).attr("tagName") == "LI") {
                            if (list.find("li:first").prev().attr("tagName") != "DIV") {
                                list.find("div").remove();
                                $('<div style="cursor: move; height: 3px; background-color: rgb(51, 102, 255); overflow: hidden;"></div>').insertBefore(list.find("li:first"));
                            }
                        }
                        else {
                            if ($(this).parent().next().attr("tagName") != "DIV") {
                                list.find("div").remove();
                                $('<div style="cursor: move; height: 3px; background-color: rgb(51, 102, 255); overflow: hidden;"></div>').insertAfter($(this).parent());
                            }
                        }

                    }
                    var mouseUp = function (e) {
                        list.find("a").unbind("mousemove", mouseMove);
                        $(".frititle li").unbind("mousemove", mouseMove);
                        $(document).unbind("mouseup", mouseUp);
                        var drag = list.find("div");
                        if (drag[0]) {
                            var selone = list.find(".down").parent();
                            var clone = selone.clone();
                            selone.remove();

                            clone.insertAfter(drag).find("a").mousedown(mouseDown);
                            drag.remove();
                            preview();
                            enableBtn(list.find(".down").parent());
                        }
                        if (this.style)
                            this.style.MozUserSelect = "";
                    }
                    list.html("");
                    if (o.Data.length > 0) {
                        for (var i = 0; i < o.Data.length; i++) {
                            var li = $('<li><a gid="{id}" href="javascript:void(0)"  style="">{name}</a></li>'.replace(/\{name\}/g, o.Data[i].Name).replace(/\{id\}/g, o.Data[i].ID));
                            list.append(li);
                            li.find("a").mousedown(mouseDown);
                            if (i < 3) {
                                var li1 = $('<li>{name}</li><li class="MIB_line_l">|</li>'.replace(/\{name\}/g, o.Data[i].Name));
                                li1.insertBefore(top.find("li:last"));
                            }
                        }
                    }
                }
                else if (o.Code == "A00001") {
                    App.showLoginDial();
                }
                else {
                    App.fullTip(CodeList["A00003"], 1000, null, 1);
                }
            },
            error: function () {
                App.fullTip(CodeList["A00003"], 1000, null, 1);
            }
        });
    }

});
    